// 微信机器人
function wxRobot(appIndex) {
    // 第几个微信，默认第一个
    this.appIndex = appIndex;
    
    // 打开【微信】标签页
    this.openWxTab = function() {
        const appName = "微信";
        console.log(this.appIndex ? "启动双开微信第 " + this.appIndex + " 个" : "启动微信");
        home();
        sleep(500);
        this.toWxMainActivity();
        sleep(3000);
        while ("com.tencent.mm.ui.LauncherUI" !== currentActivity()) {
            if (this.appIndex) {
                if (!text("请选择要使用的应用").findOnce()) {
                    console.log("当前是【%s】界面, 等待唤起双开应用选择界面...", getAppName(currentPackage()));
                    sleep(3000);
                    continue;
                }
                let btn = text("请选择要使用的应用").findOnce();
                let b = btn.bounds();
                // 点击下面图标，通过id、desc、text查询在不同时间段会出现不一样结果
                click(device.width/4 * (this.appIndex==1 ? 1 : 3), b.centerY() + 200);
                log("选择第 %s 个微信", this.appIndex);
                sleep(1000);
                continue;        
            }
            // 等待
            sleep(3000);
            console.log("当前是【%s】界面, 启动【微信】", getAppName(currentPackage()));
        }
        log("已打开微信主界面");
        return this;
    }
    // 跳转到微信主界面
    this.toWxMainActivity = function() {
        context.startActivity(app.intent({
            action: "VIEW",
            className: "com.tencent.mm.ui.LauncherUI",
            packageName: "com.tencent.mm"
          }).setFlags(335544320));
    }
    // 打开搜索
    this.openSearch = function() {
        // 切换到【微信】标签页
        this.openWxTab();
       
        let btn = id("com.tencent.mm:id/civ").findOne();
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        
        // 点击【搜索】按钮
        btn = id("com.tencent.mm:id/f0f").findOne();
        b = btn.bounds();
        click(b.centerX(), b.centerY());
        log("进入搜索页面");
        // 等待页面加载
        sleep(1000);    
    }
    // 输入
    this.write = function(str) {
        setText(str);
    }
    // 打开群聊
    this.openGroup = function() {
        let groups = id("com.tencent.mm:id/cek").findOne().parent().parent().parent();
        log(groups);
        groups.forEach(element => {
            log(element)
        });            
    }
    // 打开通讯录界面
    this.openAddressBook = function() {
        this.openWxTab();
        //let btn = id("com.tencent.mm:id/civ").text("通讯录").findOne(3000);
        let btn = className("android.widget.TextView").text("通讯录").findOne(5000);
        if (!btn) {
            log("找不到 通讯录");
            return false;
        }
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        log("打开【通讯录】界面");
        sleep(1000);
    }
    // 打开公众号
    this.openGzh = function(name) {
        this.openAddressBook();
        // 点击【公众号】入口
        btn = className("android.widget.TextView").text("公众号").findOne();
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        log("选择【公众号】类型");
        sleep(1000);

        // 点击【搜索】按钮
        btn = desc("搜索").findOne();
        b = btn.bounds();
        click(b.centerX(), b.centerY());
        log("选择【搜索】按钮");
        sleep(1000);

        // 搜索
        this.write(name);
        log("输入公众号名称");
        sleep(500);

        // 点击第二个
        btn = text(name).find()[1]
        if (!btn) {
            throw new Error("公众号【" + name + "】未关注");
            return false;
        }
        b = btn.bounds();
        click(b.centerX(), b.centerY());
        log("选择公众号");
        return true;
    }
    // 给微信公众号发消息
    this.sendTextToGzh = function(name, str) {
        try {
            if (!this.openGzh(name)) {
                log("无法给公众号发消息");
                return false;
            }
            let btn = id("amb").text("发送").findOne(1500);
            if (!btn) {
                // 非输入栏，切换
                btn = id("com.tencent.mm:id/ake").findOne(1000);
                if (!btn) {
                    btn = id("com.tencent.mm:id/aku").findOne(1000);
                }
                let b = btn.bounds();
                click(b.centerX(), b.centerY());
                log("切换到输入栏");
            }
            this.write(str);
            log("输入并发送");
            btn = className("android.widget.Button").text("发送").findOne();
            btn.click();
            back();
            sleep(800);
            back();
            sleep(800);
            back();
            sleep(800);
            return true;
        } catch (e) {
            toastLog(e.message);
            return false;
        }
    }
}

auto.waitFor();
// 第一个微信号
let wx1 = new wxRobot(1);
wx1.sendTextToGzh("毛球岛FluffyLand", "签到");
// 第二个微信号
let wx2 = new wxRobot(2);
wx2.sendTextToGzh("毛球岛FluffyLand", "签到");
// 返回主界面
home();